/*
 Rilego - An easy Images 2 E-Book converter.
Copyright (C) 2011  Federico Improta (Kirys)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/

package it.kirys.rilego.engine.loaders;

import it.kirys.rilego.engine.loaders.imagesources.FileImageSource;
import it.kirys.rilego.engine.loaders.imagesources.IImageSource;
import it.kirys.rilego.engine.loaders.imagesources.ImageType;
import it.kirys.rilego.engine.project.Project;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Federico Improta
 */
public class DiskFileLoader extends AbstractLoader{
	File location;
	final static String separator=File.pathSeparator;
	@Override
	public String getIdentifier() {
		if (location!=null){
			return location.getAbsolutePath();
		} else {
			return "";
		}
	}

	public static String concatenatePaths(String a,String b){
		String aw=a.trim();
		String bw=b.trim();

		if (!aw.endsWith(separator)){
			aw=aw.concat(separator);
		}
		if (!bw.startsWith(separator)){
			bw=bw.substring(1);
		}
		return aw.concat(bw);
	}//public static String concatenatePaths(String a,String b)

	@Override
	public String generateSourceFullIdentifier(IImageSource source) {
		if (location!=null){
			return concatenatePaths(getIdentifier(), source.getIdentifier());
		}else {
			return source.getIdentifier();
		}
	}//public String getSourceIdentifier(IImageSource source)
	

	public DiskFileLoader(File[] files) {
		super();
		for (File f:files){
			importSource(f);
		}
	}

	public DiskFileLoader(File file) {
		this (new File[]{file});
	}

	/**
	 * Populate a File list with all the known readable images of a dir recursively.
	 * @param dir directory to explore
	 * @param dest destination file list
	 * @param recursiveSteps how many recursive steps (0 means to not recurse so only files of dir will be added);
	 */
	private void populateFromDir (File dir,List<File> dest, int recursiveSteps){
		if (dir.isDirectory()){
			File[] files =dir.listFiles();
			for (File f: files){
				if ((f.isFile()) && (f.canRead()) && (ImageType.isKnown(f))){
					dest.add(f);
				} else if (f.isDirectory() && recursiveSteps>0){
					populateFromDir(f, dest, recursiveSteps-1);
				}
			}
		} else {
			return;
		}
	}//private void populateFromDir (File dir,List<File> dest, int recursiveSteps)

	/**
	 * Directory based constructor
	 * @param dir dir to use as source
	 * @param recursiveSteps max recursive steps
	 */
	public DiskFileLoader(File dir, int recursiveSteps) {
		location=dir;
		ArrayList<File> files = new ArrayList<File>();
		populateFromDir(dir, files, recursiveSteps);
		importSources(files.toArray(new File[]{}));
	}//public DiskFileLoader(File dir, int recursiveSteps)


	private void importSource(File f){
		if (f.exists()){
			FileImageSource source= new FileImageSource(this, f, ImageType.getFromFileName(f.getName()));
			sources.add(source);
		}
	}//private void importSource(File f)

	public String getRelativePath(FileImageSource source){
		if (source.getLoader()==this){
			if (location!=null){
				return source.getLocation().getAbsolutePath().substring(location.getAbsolutePath().length());
			} else {
				return source.getLocation().getAbsolutePath();
			}
		} else {
			//TODO: should raise and exception!
			return null;
		}
	}
	private void importSources(File[] files){
		for (File f:files){
			importSource(f);
		}
	}//private void importSources(File[] files)


}
